#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setMouseTracking(true);
    ini();
    iniCenter();
    iniTcp();//*/

}

Widget::~Widget()
{
    delete ui;
}

void Widget::ini()
{
    this->setWindowTitle(tr("名字叫控制台吧"));
    this->setFixedSize(MWINHEIGHT, MWINWIDTH);
    //音视频区
    labelPicture->resize(640,480);
    labelIP->move(10,10);
    QPalette label_palette;
    label_palette.setColor(QPalette::Background, QColor(0, 0, 0));
    labelPicture->setAutoFillBackground(true);
    labelPicture->setPalette(label_palette);//*/

    /*QPixmap pixVideo("img/play.png");
    btnVideoideo->resize(50,50);
    btnVideoideo->setIcon(pixVideo);
    btnVideoideo->setIconSize(pixVideo.size());
    btnVideoideo->setMask(pixVideo.mask());
    btnVideoideo->move(305, 400);//*/
    setBtnBack(btnVideoideo, "img/play.png", 305, 400);

    labelVoice->setText("点击开启录音");
    labelVoice->move(20, 510);

    /*QPixmap pixVoice("img/play.png");
    btnVoice->resize(50,50);
    btnVoice->setIcon(pixVoice);
    btnVoice->setIconSize(pixVoice.size());
    btnVoice->setMask(pixVoice.mask());
    btnVoice->move(20, 550);//*/
    setBtnBack(btnVoice, "img/play.png", 20, 550);


    //网络区
    labelIP->setText(tr("ip"));
    labelPort->setText(tr("端口"));
    labelIP->setAlignment(Qt::AlignCenter);
    labelPort->setAlignment(Qt::AlignCenter);
    lineIP->setText(tr("192.168.0.104"));
    linePort->setText(tr("8888"));
    labelIP->move(200,500);
    labelPort->move(200,560);
    lineIP->move(250,500);
    linePort->move(250,560);

    btnNetwork->setText(tr("点击链接"));
    btnNetwork->move(240,610);

    //环境区
    labelEnvironment->setText(tr("环境区"));
    labelEnvironment->move(800,20);
    labelTemp->setText(strCat("温度: ",temp));
    labelHum->setText(strCat("湿度: ",hum));
    labelLight->setText(strCat("光强: ",light));
    labelTemp->move(700,100);
    labelHum->move(700,150);
    labelLight->move(700,200);

    btnEnvironment->setText(tr("开始采集数据"));
    btnEnvironment->move(820,150);

    //控制区
    labelControl->setText(tr("控制区"));
    labelControl->move(760,330);

    /*QPixmap pixLed("img/led_on.png");
    btnLed->resize(153,153);
    btnLed->setIcon(pixLed);
    btnLed->setIconSize(pixLed.size());
    btnLed->setMask(pixLed.mask());
    //btnLed->move(20, 550);
    //btnLed->setText(tr("开关灯"));
    btnLed->move(670,350);//*/

    //突然发现是重复性的，于是写了个函数包装
    setBtnBack(btnLed, "img/led_on.png", 670, 350);

    /*QPixmap pixFan("img/fan_on.png");
    btnFan->resize(pixFan.size());
    btnFan->setIcon(pixFan);
    btnFan->setIconSize(pixFan.size());
    btnFan->setMask(pixFan.mask());
    //btnFan->setText(tr("开关风扇"));
    btnFan->move(700,400);//*/

    setBtnBack(btnFan, "img/fan_on.png", 830, 350);

    setBtnBack(btnBuzzer, "img/buz_on.png", 670, 510);

    setBtnBack(btnDoor, "img/door_on.png", 830, 510);

}

void Widget::iniCenter()
{
    //LED
    connect(btnLed,&QPushButton::clicked,[=]{
        if(isLed){
            setBtnBack(btnLed, "img/led_on.png", 670, 350);
            tcpSocket->write("led_off");
            isLed = false;
        }else{
            setBtnBack(btnLed, "img/led_off.png", 670, 350);
            tcpSocket->write("led_on");
            isLed = true;
        }
    });
    //Fan
    connect(btnFan,&QPushButton::clicked,[=]{
        if(isFan){
            setBtnBack(btnFan, "img/fan_on.png", 830, 350);
            tcpSocket->write("fan_off");
            isFan = false;
        }else{
            setBtnBack(btnFan, "img/fan_off.png", 830, 350);
            tcpSocket->write("fan_on");
            isFan = true;
        }
    });
    //Buzzer
    connect(btnBuzzer,&QPushButton::clicked,[=]{
        if(isBuz){
            setBtnBack(btnBuzzer, "img/buz_on.png", 670, 510);
            tcpSocket->write("buz_off");
            isBuz = false;
        }else{
            setBtnBack(btnBuzzer, "img/buz_off.png", 670, 510);
            tcpSocket->write("buz_on");
            isBuz = true;
        }
    });
    //Door
    connect(btnDoor,&QPushButton::clicked,[=]{
        if(isDoor){
            setBtnBack(btnDoor, "img/door_on.png", 830, 510);
            tcpSocket->write("door_off");
            isDoor = false;
        }else{
            setBtnBack(btnDoor, "img/door_off.png", 830, 510);
            tcpSocket->write("door_on");
            isDoor = true;
        }
    });
    //Videoideo
    connect(btnVideoideo,&QPushButton::clicked,[=]{
        if(isVideo){
            setBtnBack(btnVideoideo, "img/play.png", 305, 400);
            isVideo = false;
            QString ip = lineIP->text();
            qint16 port = linePort->text().toInt();
            tcpV->connectToHost(ip, port);
        }else{
            setBtnBack(btnVideoideo, "img/stop.png", 305, 400);
            isVideo = true;
        }//*/
    });
    //Voice
    connect(btnVoice,&QPushButton::clicked,[=]{
        if(isVoice){
            setBtnBack(btnVoice, "img/play.png", 20, 550);
            isVoice = false;
        }else{
            setBtnBack(btnVoice, "img/stop.png", 20, 550);
            isVoice = true;
        }
    });
    //Environment
    connect(btnEnvironment,&QPushButton::clicked,[=]{
        if(!isEnvironmentSocket){
            QString ip = lineIP->text();
            qint16 port = linePort->text().toInt();
            EnvironmentSocket->connectToHost(ip, port);
        }else {
            btnEnvironment->setText(tr("开始采集数据"));
            EnvironmentSocket->close();
            isEnvironmentSocket = false;
        }
    });

    //网络
    connect(btnNetwork, &QPushButton::clicked,[=]{
        if(!isTcpSocket){
            QString ip = lineIP->text();
            qint16 port = linePort->text().toInt();
            tcpSocket->connectToHost(ip, port);
        }else {
            tcpSocket->close();
            btnNetwork->setText("点击链接");
            isTcpSocket = false;
        }
    });

}

void Widget::iniTcp()
{
    //img
    connect(tcpSocket,&QTcpSocket::connected,[=]{
        qDebug("image链接成功");
        isTcpSocket = true;
        btnNetwork->setText("断开连接");
        tcpSocket->write("led_off");
        tcpSocket->write("fan_off");
        tcpSocket->write("buz_off");
        tcpSocket->write("door_off");
        setBtnBack(btnLed, "img/led_on.png", 670, 350);
        setBtnBack(btnFan, "img/fan_on.png", 830, 350);
        setBtnBack(btnBuzzer, "img/buz_on.png", 670, 510);
        setBtnBack(btnDoor, "img/door_on.png", 830, 510);
        isLed = false;
        isFan = false;
        isBuz = false;
        isDoor = false;
    });
    connect(tcpSocket,&QTcpSocket::readyRead,[=]{
        //qDebug("读取");
        if(isIntImg){
            if(tcpSocket->bytesAvailable()<4){
                qDebug("大小");
                return;
            }

            tcpSocket->read(buf_len, 32);
            size = atoi(buf_len);
            qDebug("size:%d",size);
            isIntImg=false;
        }
        else{
            if(tcpSocket->bytesAvailable()<size){
                qDebug("数据");
                return;
            }
            qDebug("加载图片中...");
            tcpSocket->read(img,size);
            QPixmap pixmap;
            pixmap.loadFromData((uchar *)img,size,"JPEG");

            labelPicture->setScaledContents(true);
            labelPicture->setPixmap(pixmap);
            isIntImg = true;
        }//*/
    });

    //Environment
    connect(EnvironmentSocket,&QTcpSocket::connected,[=]{
        qDebug("Environment链接成功");
        isEnvironmentSocket = true;
        btnEnvironment->setText("停止采集数据");
    });
    connect(EnvironmentSocket,&QTcpSocket::readyRead,[=]{
        if(EnvironmentSocket->bytesAvailable()<36)
            return ;
        char buf[32];
        EnvironmentSocket->read(buf, 36);
        qDebug("<< %s >>",buf);
        temp = buf[5];
        hum = buf[7];
        light = buf[20];
        labelTemp->setText(strCat("温度: ",temp));
        labelHum->setText(strCat("湿度: ",hum));
        labelLight->setText(strCat("光强: ",light));
        if(temp <= 30 && isFan == false){
            setBtnBack(btnFan, "img/fan_off.png", 830, 350);
            tcpSocket->write("fan_on");
            isFan = true;
        }else if(temp >= 80 && isFan == true){
            setBtnBack(btnFan, "img/fan_on.png", 830, 350);
            tcpSocket->write("fan_off");
            isFan = false;
        }
        if((hum <= 30 || hum >= 80) && isBuz ==false){
            setBtnBack(btnBuzzer, "img/buz_off.png", 670, 510);
            tcpSocket->write("buz_on");
            isBuz = true;
        }else if((hum >= 30 && hum <= 80) && isBuz ==true){
            setBtnBack(btnBuzzer, "img/buz_on.png", 670, 510);
            tcpSocket->write("buz_off");
            isBuz = false;
        }
        if(light <= 30 && isLed == false){
            setBtnBack(btnLed, "img/led_off.png", 670, 350);
            tcpSocket->write("led_on");
            isLed = true;

        }else if(light >= 80 && isLed == true){
            setBtnBack(btnLed, "img/led_on.png", 670, 350);
            tcpSocket->write("led_off");
            isLed = false;
        }
    });
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter *p=new QPainter(this);
    QPixmap pp = QPixmap("img/back.jpg");
    p->drawPixmap(0,0,MWINHEIGHT,MWINWIDTH,pp);
}

QString Widget::strCat(QString str, int n)
{
    QString com;
    if(str == "温度: "){
        com = "℃";
    }else if(str == "湿度: "){
        com = "RH";
    }else{
        com = "cd";
    }
    char num[4];
    sprintf(num, "%d", n);
    QString s = QString("%1%2 %3").arg(str).arg(n).arg(com);
    qDebug()<<s;
    return QString(s);
}

void Widget::setBtnBack(QPushButton *btn, QString icon, int w, int h)
{
    QPixmap pixBack(icon);
    btn->resize(pixBack.size());
    btn->setIcon(pixBack);
    btn->setIconSize(pixBack.size());
    btn->setMask(pixBack.mask());
    btn->move(w, h);
}





